﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class GoodsStorageService
    {
        /// <summary>
        /// 查询商品入库信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GoodsStorage GetGoodsStorage(long id)
        {
            GoodsStorage storage = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    storage = new GoodsStorage();
                    storage.Id = id;
                    storage = new GoodsStorageDAO().GetGoodsStorage(conn, storage);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return storage;
        }

        /// <summary>
        /// 查询商品入库信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="storeNo"></param>
        /// <param name="purchaseno"></param>
        /// <param name="barcode"></param>
        /// <param name="adminUsername"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetGoodsStorageTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String purchaseno, String barcode,
            String adminUsername, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    GoodsStorage storage = new GoodsStorage();
                    storage.Pageindex = pageindex;
                    storage.Pagesize = pagesize;
                    storage.EnterpriseNo = enterpriseNo;
                    storage.StoreNo = storeNo;
                    storage.PurchaseNo = purchaseno;
                    storage.Barcode = barcode;
                    storage.AdminUsername = adminUsername;
                    storage.Ascfield = ascfield;
                    storage.Descfield = descfield;
                    dt = new GoodsStorageDAO().GetGoodsStorageTable(conn, storage);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询商品入库信息记录数
        /// </summary>
        /// <param name="enterpriseNo"></param>
        /// <param name="storeNo"></param>
        /// <param name="purchaseno"></param>
        /// <param name="barcode"></param>
        /// <param name="adminUsername"></param>
        /// <returns></returns>
        public long GetGoodsStorageRecord(String enterpriseNo, String storeNo, String purchaseno, String barcode, String adminUsername)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    GoodsStorage storage = new GoodsStorage();
                    storage.EnterpriseNo = enterpriseNo;
                    storage.StoreNo = storeNo;
                    storage.PurchaseNo = purchaseno;
                    storage.Barcode = barcode;
                    storage.AdminUsername = adminUsername;
                    record = new GoodsStorageDAO().GetGoodsStorageRecord(conn, storage);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增商品入库信息
        /// </summary>
        /// <param name="storage"></param>
        /// <returns></returns>
        public long InsertGoodsStorage(GoodsStorage storage)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new GoodsStorageDAO().InsertGoodsStorage(conn, storage);
                    if (id > 0)
                    {
                        int flag = new GoodsService().UpdateGoodsStock(conn, storage.GoodsId, storage.StorageNumber, ActKey.ADD);
                        if (flag > 0)
                            trans.Commit();
                        else
                            throw new Exception("update goods stock add error!");
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteGoodsStorage(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        GoodsStorageDAO storageDAO = new GoodsStorageDAO();
                        GoodsStorage storage = new GoodsStorage();
                        foreach (String lopid in arr)
                        {
                            storage.Id = Int64.Parse(lopid);
                            count += storageDAO.DeleteGoodsStorage(conn, storage);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
